/**
 * $id$
 */
package org.ishequ.miduo;

import org.ishequ.miduo.R;
import org.ishequ.miduo.MiDuoActivity.ConfigHelper;
import org.ishequ.miduo.weibo.WeiboShareActivity;
import weibo4android.Weibo;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Base class for request Activity in the weibo application
 */
public class BaseActivity extends Activity {
	/**
	 * 根布局对象
	 */
	protected LinearLayout root;

	/**
	 * 标题栏左边logo按钮
	 */
	protected ImageView titlebarLogoImage;

	/**
	 * 标题栏中间文字
	 */
	protected TextView titlebarText;

	/**
	 * 标题栏右边按钮
	 */
	protected ImageView titlebarReturnButton;
	
	/**
	 * 标题栏右边操作按钮
	 */
	protected ImageView titlebarDoButton;

	private ProgressDialog progressDialog;

	/**
	 * 调用SDK接口的Weibo对象
	 */
	protected Weibo weibo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置不显示标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置布局
		setContentView(R.layout.weibo_base_layout);
		// 初始化控件
		root = (LinearLayout) findViewById(R.id.weibo_root);
		titlebarLogoImage = (ImageView) findViewById(R.id.weibo_titlebar_logo);
		titlebarReturnButton = (ImageView) findViewById(R.id.weibo_titlebar_returnBtn);
		titlebarDoButton = (ImageView) findViewById(R.id.weibo_titlebar_doBtn);
		titlebarText = (TextView) findViewById(R.id.weibo_titlebar_text);
		
		//注册“logo”按钮的事件 用于下拉列表的选择
		titlebarLogoImage.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this, AppListActivity.class);
				BaseActivity.this.startActivity(intent);
			}
		});

		// 注册“操作”按钮事件(子类若有不同的处理，则需覆盖此事件)
		titlebarDoButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this, WeiboShareActivity.class);
				BaseActivity.this.startActivity(intent);
			}
		});

		// 注册“返回”按钮事件(子类若有不同的处理，则需覆盖此事件)
		titlebarReturnButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		Intent intent = getIntent();
		weibo = (Weibo)intent.getSerializableExtra("Weibo");
		if (weibo == null) {
			weibo = new Weibo();
			weibo.setToken(ConfigHelper.nowUser.getToken(), ConfigHelper.nowUser.getTokenSecret());
		}
	}

	/**
	 * 解析逗号分割的字符串
	 * 
	 * @return
	 */
	protected String[] parseCommaIds(String s) {
		if (s == null) {
			return null;
		}
		String[] ids = s.split(",");
		return ids;
	}

	/**
	 * 显示等待框
	 */
	protected void showProgress() {
		showProgress("Please wait", "progressing");
	}

	/**
	 * 显示等待框
	 * 
	 * @param title
	 * @param message
	 */
	protected void showProgress(String title, String message) {
		progressDialog = ProgressDialog.show(this, title, message);
	}

	/**
	 * 取消等待框
	 */
	protected void dismissProgress() {
		if (progressDialog != null) {
			try {
				progressDialog.dismiss();
			} catch (Exception e) {

			}
		}
	}

	/**
	 * 显示Toast提示
	 * 
	 * @param message
	 */
	protected void showTip(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 不销毁ProgressDialog会出现view not attached to window manager异常
		dismissProgress();
	}

}
